<html>
<title>service worker registration</title>
<script>
async function register(worker_url, scope) {
  try {
    const init = scope ? {scope} : {};
    await navigator.serviceWorker.register(worker_url, init);
    await navigator.serviceWorker.ready;
    return 'DONE';
  } catch (error) {
    return `${error}`;
  }
}

async function unregister(scope) {
  try {
    const registration = await navigator.serviceWorker.getRegistration(scope);
    if (!registration)
      return 'REGISTRATION NOT FOUND';
    registration.unregister();
    return 'DONE';
  } catch (error) {
    return `${error}`;
  }
}

async function claim() {
  try {
    const registration = await navigator.serviceWorker.ready;
    const claimed = new Promise(resolve => {
      navigator.serviceWorker.oncontrollerchange = (event) => {
        resolve();
      };
    });
    registration.active.postMessage('claim');
    await claimed;
    return 'DONE';
  } catch (error) {
    return `${error}`;
  }
}
</script>
</html>
